package org.huangrui.spark.java.core.rdd.operate.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import scala.Tuple2;

import java.util.Arrays;

/**
 * @Author hr
 * @Create 2024-10-17 18:00
 */
public class Spark12_Operate_Transform_KV_ReduceBykey {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local[*]");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        JavaPairRDD<String, Integer> rdd = jsc.parallelizePairs(
                Arrays.asList(
                        new Tuple2<>("a", 1),
                        new Tuple2<>("b", 2),
                        new Tuple2<>("a", 3),
                        new Tuple2<>("b", 4)
                )
        );
        // TODO 将分组聚合功能进行简化操作
        //     reduceByKey方法的作用：将KV类型的数据按照 K 对 V 进行reduce（将多个值聚合成一个值）操作
        //         [1,2,3,4] => 10
        //     计算的基本思想：两两计算
        //     (i1, i2) => i3
        rdd.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        }).collect().forEach(System.out::println);

        rdd.reduceByKey(Integer::sum).collect().forEach(System.out::println);

        jsc.close();
    }
}
